package stack_queue.p1_stack.arraystack;

import java.util.Arrays;

public class ArrayStack {
    public int[] data;
    public int top = -1;

    public ArrayStack() {
        this.data = new int[10];
    }

    public void push(int val) {
        if (isFull()) {
            this.data = Arrays.copyOf(data,  data.length + (data.length >> 1));
        }
        data[++top] = val;
    }

    public boolean isFull() {
        return top == data.length - 1;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return data[top--];
    }

    public int getSize() {
        return top + 1;
    }

    public int peek() {
        return data[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }
}
